<?php

namespace common\models\shop;

use Yii;

/**
 * This is the model class for table "{{%shop_product_stats}}".
 *
 * @property int $id
 * @property int $product_id 商品id
 * @property int $views 浏览量
 * @property int $sales 销售量
 * @property int $weights 权重
 * @property int $recommend_time 推荐时间
 * @property int $hot_time 热销时间
 * @property int $on_time 上架时间
 * @property int $off_time 下架时间
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class ShopProductStats extends \common\db\ActiveRecord {

    const WIDGETS_PAY = 10;
    const WIDGETS_VIEW = 1;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_product_stats}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['product_id', 'views', 'sales', 'weights', 'recommend_time', 'hot_time', 'on_time', 'off_time', 'created_at', 'updated_at'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'product_id' => '商品id',
            'views' => '浏览量',
            'sales' => '销售量',
            'weights' => '权重',
            'recommend_time' => '推荐时间',
            'hot_time' => '热销时间',
            'on_time' => '上架时间',
            'off_time' => '下架时间',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function beforeValidate() {
        if (!is_numeric($this->recommend_time)) {
            $this->recommend_time = strtotime($this->recommend_time);
        }
        return parent::beforeValidate();
    }

    public function getGoods() {
        return $this->hasOne(ShopProduct::className(), ['id' => 'product_id']);
    }
    
    

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'views',
            'sales',
            'recommend_time',
            'hot_time',
            'on_time',
            'off_time',
        ];
    }

}
